package com.tomehay.termprojectmanage_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tomehay.termprojectmanage_server.enums.ResponseCode;
import com.tomehay.termprojectmanage_server.mapper.GroupTaskMapper;
import com.tomehay.termprojectmanage_server.pojo.Model.GroupTask;
import com.tomehay.termprojectmanage_server.pojo.Model.TaskGrades;
import com.tomehay.termprojectmanage_server.mapper.TaskGradesMapper;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.TaskGradesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tomehay
 * @since 2023-06-20
 */
@Service
public class TaskGradesServiceImpl extends ServiceImpl<TaskGradesMapper, TaskGrades> implements TaskGradesService {

    @Resource
    TaskGradesMapper taskGradesMapper;

    @Resource
    GroupTaskMapper groupTaskMapper;

    @Override
    public ResultVO<Void> ScoreTask(int score, int id) {
        TaskGrades taskGrades = new TaskGrades();
        taskGrades.setGroupTaskId(id);
        taskGrades.setCreateTime(LocalDateTime.now());
        taskGrades.setTeacherGrades(score);

        taskGradesMapper.insert(taskGrades);

        UpdateWrapper<GroupTask> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status",2);
        updateWrapper.eq("id",id);
        groupTaskMapper.update(null,updateWrapper);
        return new ResultVO<>(ResponseCode.OK);
    }

    @Override
    public ResultVO<Integer> GetScore(int id) {
        QueryWrapper<TaskGrades> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_task_id",id);
        TaskGrades taskGrades = taskGradesMapper.selectOne(queryWrapper);

        return new ResultVO<>(ResponseCode.auto(taskGrades.getTeacherGrades()),taskGrades.getTeacherGrades());
    }

    @Override
    public ResultVO<List<TaskGrades>> getByCourseID(int courseId) {
        List<TaskGrades> taskGrades = taskGradesMapper.getByCourseID(courseId);
        return new ResultVO<>(ResponseCode.auto(taskGrades),taskGrades);
    }
}
